package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.CraftsmanCompanyQO;
import com.xingkeduo.pojo.table.CraftsmanCompanyTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

/**
 * 一线员工账户封装类
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:45:17
 * @description
 */
@Component
public class CraftsmanCompanyJTO extends BaseJson {
    public static CraftsmanCompanyTO createTableForNew(String json) throws Throwable {
        CraftsmanCompanyTO table = new CraftsmanCompanyTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setCompanyId(validPrimary(root,"companyId", YesNoEnum.NO)).setCraftsmanId(validPrimary(root,"craftsmanId",YesNoEnum.NO));

        return table;
    }

    public static CraftsmanCompanyQO createQueryForList(String json) throws Throwable {
        CraftsmanCompanyQO query = new CraftsmanCompanyQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setCompanyId(validPrimary(root,"companyId", YesNoEnum.NO)).setCraftsmanId(validPrimary(root,"craftsmanId",YesNoEnum.NO));
        return query;
    }

    public static CraftsmanCompanyQO createQueryForPrimary(String json) throws Throwable {
        CraftsmanCompanyQO query = new CraftsmanCompanyQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validId(root, YesNoEnum.YES));

        return query;
    }

    public static ObjectNode toJson(CraftsmanCompanyTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (table != null) {
            root.put("craftsmanId", table.getCraftsmanId());
            root.put("companyId", table.getCompanyId());
        }
        toBaseJson(root,table);
        return root;
    }

    public static ArrayNode toJson(List<CraftsmanCompanyTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<CraftsmanCompanyTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }

        return root;
    }

}
